#include "comm.hpp"
int main()
{
    //创建共享内存
    int shmid = CreateShm();
    printf("共享内存创建成功\n");

    //创建命名管道
    Init init;

    //打开命名管道
    int fd = open("mkfifo", O_RDONLY, 0666);
    if(fd == -1)
    {
        log(Fatal, "error string: %s, error code: %d", strerror(errno), errno);
        exit(MKFIFO_OPEN_ERR);
    }

    //挂接到进程
    char* shmaddr = (char*)shmat(shmid, nullptr, 0);

    //进程间通信
    // while(true)
    // {
    //     char buffer[1024] = {'\0'};
    //     int s = read(fd, buffer, sizeof(buffer));
    //     if(s > 0)
    //     {
    //         cout << "client say:";
    //         cout << shmaddr << endl;
    //     }
    //     else if(s == 0)  break;
   
    //     sleep(1);
    // }

    while(true)
    {
        char c;
        int s= read(fd, &c, 1);
        if(s == 0)  break;
        cout << "clien say:";
        cout << shmaddr;
        sleep(1);
    }

    //去关联
    shmdt(shmaddr);

    //释放共享内存
    shmctl(shmid, IPC_RMID, nullptr);
    cout << "释放共享内存成功" << endl;

    // struct shmid_ds shm_ds;
    // shmctl(shmid, IPC_STAT,&shm_ds);
    // cout << "shm_cpid:" << shm_ds.shm_cpid << endl;
    // cout << "key:" << shm_ds.shm_perm.__key << endl;
    //printf("mode:0x%x\n", shm_ds.shm_perm.mode);
}